﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

namespace JX3Answering.Common
{
    public class CommonMethod
    {
        /// <summary>
        /// 关键字优化
        /// </summary>
        /// <param name="keyWord">关键字</param>
        /// <returns></returns>
        public static string KeyWordOptimization(string keyWord)
        {
            string resultStr = keyWord.Trim().Replace(":", "：")
                .Replace("?", "？")
                .Replace(",", "，")
                .Replace(". ", "·")
                .Replace(" .", "·")
                .Replace("(", "（")
                .Replace(")", "）")
                .Replace(";","；");
            if (resultStr.Contains(@""""))
            {
                resultStr = ReplaceQuatos(resultStr);
            }
            if (resultStr.Contains("_") && !resultStr.Contains("__"))
            {
                resultStr.Replace("_", "______");
            }
            if (resultStr.Contains("__") && !resultStr.Contains("___"))
            {
                resultStr.Replace("__", "______");
            }
            if (resultStr.Contains("___") && !resultStr.Contains("____"))
            {
                resultStr.Replace("___", "______");
            }
            if (resultStr.Contains("____") && !resultStr.Contains("___"))
            {
                resultStr.Replace("____", "______");
            }
            return resultStr;
        }

        /// <summary>
        /// 英文引号转换为中文引号
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string ReplaceQuatos(string content)
        {
            //把字符串按照双引号截成数组
            string[] str = content.Split('\"');
            //替换后的字符串
            string Newstr = "";
            for (int i = 1; i <= str.Length; i++)
            {
                if (i % 2 == 0)
                {
                    Newstr += str[i - 1] + "”";
                }
                else
                {
                    Newstr += str[i - 1] + "“";
                }
            }
            return Newstr.Substring(0, Newstr.Length - 1);
        }

        public static string DailyQueryKeyword(string keyword)
        {
            if (keyword.Contains("题："))
            {
                return keyword.Substring(4);
            }
            return keyword;
        }


        /// <summary>
        /// 判断图片是否一致
        /// </summary>
        /// <param name="bitmapSource">图片一</param>
        /// <param name="bitmapTarget">图片二</param>
        /// <returns>是否一致</returns>
        public static bool IsSameImg(Bitmap bitmapSource)
        {
            try
            {
                Bitmap bitmapTarget = new Bitmap(@".\Picture\title.jpg");
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                int countSame = 0;
                int countDifferent = 0;
                for (int i = 0; i < bitmapTarget.Width; i++)
                {
                    for (int j = 0; j < bitmapTarget.Height; j++)
                    {
                        if (bitmapSource.GetPixel(i, j).Equals(bitmapTarget.GetPixel(i, j)))
                        {
                            countSame++;
                        }
                        else
                        {
                            countDifferent++;//不同之处，如果为0 则说明两张图片一样
                        }
                    }
                }
                stopwatch.Stop();
                bitmapTarget.Dispose();
                if (countDifferent == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {

                return false;
            }
            
        }

    }
}